package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Car;
import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.query.OrganizationQueryObject;
import cn.wolfcode.crm.service.ICarService;
import cn.wolfcode.crm.service.IClientService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("car")
public class CarController {

    @Autowired
    ICarService carService;
    @Autowired
    IClientService clientService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"car:view","车辆页面"},logical = Logical.OR)
    public String view() {
        return "car";
    }

    @RequestMapping("query")
    @ResponseBody
    @RequiresPermissions(value = {"car:query","车辆列表"},logical = Logical.OR)
    public PageResult query(OrganizationQueryObject qo) {
        return carService.query(qo);
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Car> selectAll() {
        return carService.selectAll();
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"car:saveOrUpdate","车辆新增/更改"},logical = Logical.OR)
    public JsonResult saveOrUpdate(Car dept) {
        Client client = clientService.selectByPrimaryKey(dept.getClient().getId());
        try {
            if (dept.getId() != null) {
                carService.updateByPrimaryKey(dept,client);
            } else {
                //保存新的员工要设置默认的状态为在职
                carService.insert(dept,client);
            }
            return JsonResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark("保存失败");
        }
    }
}
